#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2023-11-08 14:00:05
# @Author  : Figo Young

from tkinter import *
from tkinter import ttk

from screen_for_pdf import *


class MainWin(Tk):
    def __init__(self):
        super().__init__()
        self.title('电子书截屏器')
        self.screenwidth = self.winfo_screenwidth()  # 窗口宽
        self.screenheigh = self.winfo_screenheight()  # 窗口高
        self.mouse_point = []
        self.control_point = []
        self.filename = ''
        screenwidth = self.screenwidth / 5.5
        screenheight = self.screenheigh / 5.5
        x = (self.screenwidth - screenwidth) * 0.98
        y = (self.screenheigh - screenheight) * 0.9
        self.geometry('%dx%d+%d+%d' % (screenwidth, screenheight, x, y))  # widthxheight+x+y
        # 第一行
        self.labe1 = ttk.Label(self, text='请点击屏幕，确定截屏的位置', font=('微软雅黑', 11))
        self.labe1.place(relx=0.07, rely=0.05, anchor=NW)
        # 第二行
        self.scrlabe1 = ttk.Label(self, text='屏幕尺寸:', font=('微软雅黑', 9))
        self.scrlabe1.place(relx=0.07, rely=0.18, anchor=NW)
        tx = str(self.screenwidth) + ',' + str(self.screenheigh)
        self.scrlabe2 = ttk.Label(self, text=tx, font=('微软雅黑', 9))
        self.scrlabe2.place(relx=0.25, rely=0.18, anchor=NW)
        # 第三行
        self.labe2 = ttk.Label(self, text='开始点:', font=('微软雅黑', 9))
        self.labe2.place(relx=0.07, rely=0.3, anchor=NW)
        self.startp = StringVar()
        self.labe3 = ttk.Label(self, textvariable=self.startp, font=('微软雅黑', 9))
        self.labe3.place(relx=0.2, rely=0.3, anchor=NW)

        self.labe4 = ttk.Label(self, text='终 点:', font=('微软雅黑', 9))
        self.labe4.place(relx=0.37, rely=0.3, anchor=NW)
        self.endp = StringVar()
        self.labe5 = ttk.Label(self, textvariable=self.endp, font=('微软雅黑', 9))
        self.labe5.place(relx=0.47, rely=0.3, anchor=NW)
        # 第四行
        self.labe6 = ttk.Label(self, text='控制点:', font=('微软雅黑', 9))
        self.labe6.place(relx=0.66, rely=0.3, anchor=NW)
        self.control_poit = StringVar()
        self.labe7 = ttk.Label(self, textvariable=self.control_poit, font=('微软雅黑', 9))
        self.labe7.place(relx=0.79, rely=0.3, anchor=NW)

        # 第二行
        self.labe8 = ttk.Label(self, text='页 数：', font=('微软雅黑', 10))
        self.labe8.place(relx=0.07, rely=0.45, anchor=NW)
        self.pageno = IntVar()
        self.entry1 = ttk.Entry(self, textvariable=self.pageno, width=15)
        self.entry1.place(relx=0.22, rely=0.45)
        self.pageno.set(0)
        # 第五行
        self.labe9 = ttk.Label(self, text='文件名：', font=('微软雅黑', 10))
        self.labe9.place(relx=0.07, rely=0.6, anchor=NW)
        self.filename = StringVar()
        self.entry2 = ttk.Entry(self, textvariable=self.filename, width=25)
        self.entry2.place(relx=0.22, rely=0.6)
        self.filename.set('PDFEbook')

        # 第三行 按钮

        self.botten1 = Button(self, text='点击屏幕', width=10, height=1, command=self.button1)
        self.botten1.place(relx=0.05, rely=0.77, anchor=NW)
        self.botten2 = Button(self, text='控制点', width=9, height=1, command=self.button2)
        self.botten2.place(relx=0.31, rely=0.77, anchor=NW)
        self.botten3 = Button(self, text='截屏', width=8, height=1, command=self.button3)
        self.botten3.place(relx=0.56, rely=0.77, anchor=NW)
        self.botten4 = Button(self, text='保存', width=8, height=1, command=self.button4)
        self.botten4.place(relx=0.78, rely=0.77, anchor=NW)
        # 内饰 /////////// 截屏开始点  结束点
        # 页数
        # 按钮： 开始 暂停 结束

    def button1(self):  # 点击屏幕
        self.mouse_point = GetPoint().get_poit(2)  # 获得鼠标点
        me = str(self.mouse_point[1][0]) + ',' + str(self.mouse_point[1][1])
        ms = str(self.mouse_point[0][0]) + ',' + str(self.mouse_point[0][1])

        self.endp.set(me)
        self.startp.set(ms)

    def button2(self):  # 控制点
        self.control_point = GetPoint().get_poit(1)  # 获得鼠标点
        ms = str(self.control_point[0][0]) + ',' + str(self.control_point[0][1])
        self.control_poit.set(ms)

    def button3(self):  # 截屏
        x = 0
        try:
            x = self.pageno.get()
        except :
            messagebox.showinfo('错误', '页数必需是整数。')
            return
        # ch = Check.check_page(x)  # 检查页码数字

        # if ch == False:
        #     messagebox.showinfo('错误', '页数必须为整数')
        #     return
        # else:
        #     x = int(x)
        # if x <= 0:
        #     messagebox.showinfo('错误', '请输入页数')
        #     return
        if len(self.control_point) <= 0:
            messagebox.showinfo('错误', '请点击屏幕中的页目录。')
            return
        if len(self.mouse_point) <= 0:
            messagebox.showinfo('错误', '请点击屏幕中页面的左上角和右下角。')
            return

        merpdf = MergePDF()  # 创建文件夹
        pdf_fold = merpdf.creat_file()  # 创建文件夹
        if pdf_fold == False:
            return
        nm = merpdf.screenshot_pic(int(x), self.mouse_point, self.control_point)  # 截屏后保存。

        png_file = os.listdir(pdf_fold)
        for f in png_file:
            if 'png' in f:
                yn = messagebox.askyesnocancel('提示', '截屏了%s页.\n 合并图像为PDF按是;打开文件夹按否;退出按取消。' % nm)
                if yn == True:
                    merpdf.save_pic_to_pdf(self.filename.get())  # 保存图像为合并的pdf文件。
                    os.startfile(pdf_fold)  # 打开PDF文件。
                    return
                elif yn == False:
                    os.startfile(pdf_fold)  # 打开PDF文件。
                    return
                else:
                    return

    def button4(self):  # 保存
        MergePDF().save_pic_to_pdf(self.filename.get())  # 把文件夹中的图片合并为PDF文件并打开
